using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using csammisrun.shaim;

namespace Controls
{
  /// <summary>
  /// Interaction logic for DataPromptList.xaml
  /// </summary>

  public partial class DataPromptList : ListBox
  {
    private int _tabindexstart = 0;

    public DataPromptList()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Gets or sets the index at which to start assigning tab indexes to rendered controls
    /// </summary>
    public int TabIndexStart
    {
      get { return _tabindexstart; }
      set { _tabindexstart = value; }
    }

    /// <summary>
    /// Gets or sets the list of PluginData items to be rendered
    /// </summary>
    public List<PluginData> DataPrompts
    {
      get { return this.ItemsSource as List<PluginData>; }
      set
      {
        // Copy the list so we don't make irreversable changes
        List<PluginData> list = new List<PluginData>();
        foreach (PluginData pd in value)
        {
          // :v
          list.Add(pd.GetType().GetConstructor(new Type[] { pd.GetType() }).Invoke(new object[] { pd }) as PluginData);
        }

        this.ItemsSource = list;

        foreach (object obj in this.ItemsSource)
        {
          StackPanel sp = this.ItemContainerGenerator.ContainerFromItem(obj) as StackPanel;
          if (sp != null)
          {
            (sp.Children[1] as Control).TabIndex = _tabindexstart++;
          }
        }
      }
    }

    private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {

    }

    private void ListBox_SelectionChanged(object sender,
      SelectionChangedEventArgs e)
    {
      this.SelectedIndex = -1;
      e.Handled = true;
    }
  }
}